<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goals_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	} 	
	
	public function get_objectives()
	{
	return $this->db->get('objectives')->result();	
	}
	
	public function create_objective($insert)
	{
	$this->db->insert('objectives',$insert);	
	}
	
	public function create_goal($insert)
	{
	$this->db->insert('goals',$insert);	
	}
	
	public function edit_goal($insert,$id)
	{
		$this->db->update('goals',$insert,array('id'=>$id));
	}
	
	public function get_goals()
	{
	return $this->db->get('goals')->result();	
	}
	
	
	 /* function of key results starts here */
	public function get_goals_by_id($id)
	{
		return $this->db->get_where('goals',array('objective_id'=>$id))->result();
	}
	
	public function get_goal_details($id)
	{
		$this->db->select('goals.*,users.username,objectives.title as obj_title');
		$this->db->from('goals');
		$this->db->where('goals.id ='.$id);
		$this->db->join('users', 'goals.user_id = users.id');
		$this->db->join('objectives', 'goals.objective_id = objectives.id');		
	 return $this->db->get()->row_array();	
	 
	 
//return $this->db->get_where('goals',array('id'=>$id))->row_array();
	}
	
	public function delete_goal($id)
	  {
		  $this->db->delete('goals',array('id'=>$id));
	  }
	
	  /* function of key results starts here */
	  
	  public function get_results($goal_id)
	  {
return $this->db->order_by('is_achieved asc,ordering asc')->get_where('results',array('goal_id'=>$goal_id))->result();
	  }
	  
	  public function create_result($insert)
	  {
		$this->db->insert('results',$insert);
	  }
	  
	   public function edit_result($id)
	{
		return $this->db->get_where('results',array('id'=>$id))->result();
	}
	
	public function check_result_is_achieved($result_id)
	{
		$this->db->select('is_achieved');
		$this->db->from('results');
		$this->db->where('results.id ='.$result_id);
		return $this->db->get()->row_array();
	}
	
	
	public function result_achieved($result_id)
	{		
		if($this->db->update('results',array('is_achieved'=>1),array('id'=>$result_id)))
		{
		return 'yes';	
		}
		else { return 'false';}
	}
	
	public function result_unachieved($result_id)
	{		
		$this->db->update('results',array('is_achieved'=>0),array('id'=>$result_id));
	}
	
	
	  public function update_result($update,$id)
	  {
		//  print_r($id);
		  //exit;
		$this->db->update('results',$update,array('id'=>$id));  
	  }
	  public function delete_result($id)
	  {
		  $this->db->delete('results',array('id'=>$id));
	  }
	  
	 public function result_soarting($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('results',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));		
				$listingCounter = $listingCounter + 1;		
			}			
		  } 
		}
		
public function completed_percentage($percentage,$id)
		{			
$this->db->update('goals',array('completed_percentage'=>$percentage),array('id'=>$id));
		}
		
		
		
		
/* note function  */
	public function add_note($insert)
	{
		$this->db->insert('goal_notes',$insert);
	}
	
	public function get_note_by_id($id)
	{
		$this->db->select('goal_notes.*,users.username,users.profile_image');
		$this->db->from('goal_notes');
		$this->db->where('goal_notes.goal_id ='.$id);
		$this->db->join('users','goal_notes.user_id = users.id');
		
	 return $this->db->get()->result();	
	}
	
	public function delete_note($id)
	{		
		if($this->db->delete('goal_notes',array('id'=>$id)))
		{
		return true;	
		}
		else { return false ; }
	}
	
	  /* note function ends here */	
	  
}
